package com.csw.android.dev_utils.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment

/**
 * 在通用Activity打开页面
 * @param fragmentCompanion Fragment伴生对象
 * @param context 上下文
 * @param data 参数
 * @param requestCode 请求码，不为空时，将通过startActivityForResult进行页面跳转
 */
fun openInCommon(
    fragmentCompanion: Any,
    context: Context,
    data: Bundle? = null,
    container: Class<*> = CommonActivity::class.java
) {
    val fragmentClass = fragmentCompanion.javaClass.enclosingClass ?: return//外部类，即为伴生类的主类类型
    val intent = Intent(context, container)
    if (context !is Activity) {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }
    data?.let {
        intent.putExtras(it)
    }
    intent.putExtra(CommonActivity.KEY_IN_FRAGMENT_CLASS, fragmentClass)
    context.startActivity(intent)
}

/**
 * 在通用Activity打开页面
 * @param fragmentCompanion Fragment伴生对象
 * @param activity 接收返回结果的页面
 * @param data 参数
 * @param requestCode 请求码，将通过startActivityForResult进行页面跳转
 */
fun openInCommon(
    fragmentCompanion: Any,
    activity: Activity,
    data: Bundle? = null,
    requestCode: Int,
    container: Class<*> = CommonActivity::class.java
) {
    val fragmentClass = fragmentCompanion.javaClass.enclosingClass ?: return//外部类，即为伴生类的主类类型
    val intent = Intent(activity, container)
    data?.let {
        intent.putExtras(it)
    }
    intent.putExtra(CommonActivity.KEY_IN_FRAGMENT_CLASS, fragmentClass)
    activity.startActivityForResult(intent, requestCode)
}

/**
 * 在通用Activity打开页面
 * @param fragmentCompanion Fragment伴生对象
 * @param fragment 接收返回结果的页面
 * @param data 参数
 * @param requestCode 请求码，将通过startActivityForResult进行页面跳转
 */
fun openInCommon(
    fragmentCompanion: Any,
    fragment: Fragment,
    data: Bundle? = null,
    requestCode: Int,
    container: Class<*> = CommonActivity::class.java
) {
    val fragmentClass = fragmentCompanion.javaClass.enclosingClass ?: return//外部类，即为伴生类的主类类型
    val intent = Intent(fragment.activity, container)
    data?.let {
        intent.putExtras(it)
    }
    intent.putExtra(CommonActivity.KEY_IN_FRAGMENT_CLASS, fragmentClass)
    fragment.startActivityForResult(intent, requestCode)
}

/**
 * Fragment伴生对象实现此接口拥有通用的open入口
 */
interface CommonOpen {

    /**
     * 在通用页面打开Fragment
     * @param context 上下文
     * @param data 页面参数Bundle
     */
    fun open(context: Context, data: Bundle? = null) {
        openInCommon(this, context, data)
    }

    fun open(activity: Activity, data: Bundle? = null, requestCode: Int) {
        openInCommon(this, activity, data, requestCode)
    }

    fun open(fragment: Fragment, data: Bundle? = null, requestCode: Int) {
        openInCommon(this, fragment, data, requestCode)
    }
}

/**
 * Fragment伴生对象实现此接口拥有通用的open入口
 */
interface TVCommonOpen {

    /**
     * 在通用页面打开Fragment
     * @param context 上下文
     * @param data 页面参数Bundle
     */
    fun open(context: Context, data: Bundle? = null) {
        openInCommon(this, context, data, TVCommonActivity::class.java)
    }

    fun open(activity: Activity, data: Bundle? = null, requestCode: Int) {
        openInCommon(this, activity, data, requestCode, TVCommonActivity::class.java)
    }

    fun open(fragment: Fragment, data: Bundle? = null, requestCode: Int) {
        openInCommon(this, fragment, data, requestCode, TVCommonActivity::class.java)
    }
}

/**
 * 自定义页面打开Fragment
 * @param container Activity Class，建议该Activity直接继承[CommonActivity]或[TVCommonActivity]
 */
open class CustomOpen(val container:Class<*>){

    fun open(context: Context, data: Bundle? = null) {
        openInCommon(this, context, data, container)
    }

    fun open(activity: Activity, data: Bundle? = null, requestCode: Int) {
        openInCommon(this, activity, data, requestCode,container)
    }

    fun open(fragment: Fragment, data: Bundle? = null, requestCode: Int) {
        openInCommon(this, fragment, data, requestCode, container)
    }
}